package tieba.core;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import tieba.entity.CmsUser;
import tieba.service.ICmsUserService;

public class CookieUtils {

	public static CmsUser isLogin(HttpServletRequest req,
			ICmsUserService cmsUserService) {
		Cookie userCookie = null;
		CmsUser user = null;
		for (Cookie cookie : req.getCookies() == null ? new Cookie[] {} : req
				.getCookies()) {
			if ("user".equals(cookie.getName()))
				userCookie = cookie;
		}
		if (userCookie != null && userCookie.getValue() != null) {
			String username = userCookie.getValue().split(":")[0];
			String password = userCookie.getValue().split(":")[1];
			return cmsUserService.login(new CmsUser(username, password));
		}
		return user;
	}
}
